<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>简单计算器</title></head>
<body><h1>简单计算器</h1>
<input type="text" id="num1" placeholder="请输入第一个数字">

<select id="operator">
    <option value="+">+</option>
    <option value="-">-</option>
    <option value="*">*</option>
    <option value="/">/</option>
</select>

<input type="text" id="num2" placeholder="请输入第二个数字">

<button onclick="calculate()">计算</button>
<br> <label>结果:</label> <span id="result"></span> <br><br>
<button onclick="appendNumber(0)">0</button>
<button onclick="appendNumber(1)">1</button>
<button onclick="appendNumber(2)">2</button>
<button onclick="appendNumber(3)">3</button>
<button onclick="appendNumber(4)">4</button>
<br>
<button onclick="appendNumber(5)">5</button>
<button onclick="appendNumber(6)">6</button>
<button onclick="appendNumber(7)">7</button>
<button onclick="appendNumber(8)">8</button>
<button onclick="appendNumber(9)">9</button>
<script> function calculate() {
    const num1 = parseFloat(document.getElementById("num1").value);
    const num2 = parseFloat(document.getElementById("num2").value);
    const operator = document.getElementById("operator").value;
    let result;
    switch (operator) {
        case "+":
            result = num1 + num2;
            break;
        case "-":
            result = num1 - num2;
            break;
        case "*":
            result = num1 * num2;
            break;
        case "/":
            result = num1 / num2;
            break;
        default:
            result = "无效的运算符";
    }
    document.getElementById("result").innerText = result;
}

/*

function appendNumber(number) {
    const numField = document.getElementById("num1");

    numField.value += number;

}

function appendNumber(number) {
    const numField = document.getElementById("num2");
    numField.value += number;
}
*/

function appendNumber(number, targetFieldId) {
    const numField1 = document.getElementById("num1");
    const numField2 = document.getElementById("num2");

    if (targetFieldId === "num1") {
        numField1.value += number;
    } else if (targetFieldId === "num2") {
        numField2.value += number;
    } else {
        console.error("Invalid target field ID: " + targetFieldId);
    }

}




</script>
</body>
</html>